package com.ruiyian.netty.DataUtils;


import com.aliyun.outboundbot20191226.models.ApplyNumberDistrictInfoParsingResultRequest;

import java.nio.charset.Charset;

public class CRCUtils {
    private static final int POLYNOMIAL = 0x1021; // CRC-16-XMODEM 标准多项式
    private static final int INITIAL = 0x0000; // 初始

    /**
     * CRC-16-XMODEM校验
     * @param bytes 待校验的值
     * @return crc校验码
     */
    public static int calculateCRC16XMODEM(byte[] bytes) {
        int crc = INITIAL;
        for (byte b : bytes) {
            crc ^= (b & 0xFF) << 8; // XOR with byte value
            for (int i = 0; i < 8; i++) {
                if ((crc & 0x8000) != 0) {
                    crc = (crc << 1) ^ POLYNOMIAL;
                } else {
                    crc <<= 1;
                }
            }
        }
        return crc & 0xFFFF;
    }

    public static String getCRCModbus(byte[] bytes) {
        // CRC寄存器全为1
        int CRC = 0x0000ffff;
        // 多项式校验值
        int POLYNOMIAL = 0x0000a001;
        int i, j;
        for (i = 0; i < bytes.length; i++) {
            CRC ^= ((int) bytes[i] & 0x000000ff);
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
        // 结果转换为16进制
        String result = Integer.toHexString(CRC).toUpperCase();
        if (result.length() != 4) {
            StringBuffer sb = new StringBuffer("0000");
            result = sb.replace(4 - result.length(), 4, result).toString();
        }
        //高位在前地位在后
        return result.substring(2, 4) + result.substring(0, 2);
        // 交换高低位，低位在前高位在后
//        return result.substring(2, 4) + result.substring(0, 2);
    }

    public static void main(String[] args) {
        String w = "谨";
        byte[] asciiArray = w.getBytes(Charset.forName("GBK")); // 获取对应的中文GBK编码数组

        for (byte b : asciiArray) {
            System.out.println(String.format("ASCII码值：%x", b));
        }
        String message = "Hello, world!";
        byte[] a = new byte[4];
        a[0] = 0x31;
        a[1] = 0x32;
        a[2] = 0x33;
        a[3] = 0x34;
        int crc16Value = calculateCRC16XMODEM(a);
        System.out.println(String.format("%x",crc16Value));
//        System.out.println("CRC-32 value: " + crc32Value);
        String secure_board_msg2 = "0230303130706C61792E6C73742B000000005B506C61796C6973745D0D0A4974656D5F4E6F3D310D0A4974656D303D3530302C312C352C5C6673333233325C433030303030305C5330305C63323535323535303030303030BDF7C9F7BCDDCABB205C6673333233325C433030303033325C5330305C63323535323535303030303030BCF5CBD9C2FDD0D0701E03";
//                                  0230303130706c61792e6c73742b000000005b506c61796c6973745dda4974656d5f4e6f3d31da4974656d303d3530302c312c352c5c66
        char[] charArr = secure_board_msg2.toCharArray();
        for(int i=0;i<charArr.length;i++){
            //
            if(i%2==0){
                int z = 0;
                int x = 0;
                if(charArr[i]<=57){
                    z = charArr[i]-48;
                }
                else if(charArr[i]<=70){
                    z = charArr[i]-54;
                }

                if(charArr[i+1]<=57){
                    x = charArr[i+1]-48;
                }
                else if(charArr[i+1]<=70){
                    x = charArr[i+1]-55;
                }

                int charnum = z*16+x;
//                System.out.println(String.format("%c%c:%c",charArr[i],charArr[i+1],charnum));
                System.out.print(String.format("%c%c:%c,",charArr[i],charArr[i+1],charnum));
            }
            else {
                continue;
            }
        }
    }
}
